﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Marmat.Forms.Design
{
    public class DefalutSmartMatchControlService : ISmartMatchControl
    {
        Type[] ISmartMatchControl.GetCollection()
        {
            List<Type> result = new List<Type>();
            System.Reflection.Assembly[] assemblies = System.AppDomain.CurrentDomain.GetAssemblies();
            foreach (System.Reflection.Assembly assembly in assemblies)
            {
                if (assembly.IsDynamic) continue;
                try
                {
                    foreach (Type type in assembly.GetExportedTypes())
                    {
                        if (type.IsPublic && type.IsAbstract == false && typeof(Control).IsAssignableFrom(type) && type.GetInterfaces().Contains(typeof(Marmat.Forms.IBindingField)))
                            result.Add(type);
                    }
                }
                catch (Exception) { }
            }
            return result.ToArray();
        }

        Type ISmartMatchControl.Match(SmartMatchControlInfo info)
        {
            Type result = null;
            Type propertyType = info.PropertyType;
            if (propertyType.IsGenericType && propertyType.GetGenericTypeDefinition() == typeof(System.Nullable<>))
                propertyType = propertyType.GenericTypeArguments[0];
            if (propertyType == typeof(string))
            {
                if (info.PropertyName.Contains("备注") || info.Caption?.Contains("备注") == true)
                    result = typeof(MemoBox);
                else
                    result = typeof(TextBox);
            }
            else if (propertyType == typeof(int) || propertyType == typeof(long) || propertyType == typeof(float) || propertyType == typeof(double) || propertyType == typeof(decimal))
                result = typeof(NumericBox);
            else if (propertyType == typeof(bool))
                result = typeof(CheckBox);
            else if (propertyType == typeof(DateTime))
                result = typeof(DateTimePicker);
            else if (info.PropertyType == typeof(System.Collections.IEnumerable))
                result = typeof(GridControl);
            else if (propertyType == typeof(byte[]))
            {
                //if (info.PropertyName.Contains("图") || info.PropertyName.Contains("照片"))
                //    result = typeof(PictureView);
            }
            return result;
        }
    }
}
